<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'wh:rd02:rd02:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="40%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
      <!-- <DictLabel v-if="record.bred" :dictType="'wms_bred_status'" :dictValue="record.bred" /> -->
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsBasInvBasInvForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { basInvUpdateYfdh } from '/@/api/bas/inv/basInv';
  import { updateWlData } from '/@/api/wms/rd/order/rdOrder';
  import { DictLabel } from '/@/components/Dict';
  import { companyTreeData } from '/@/api/sys/company';
  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('bas.inv.basInv');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<Rd02>({} as Rd02);
  const loadingRef = ref(false);
  const djDefaultValue = ref<any>('');

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: t('邮发代号批改'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('公司名称'),
      field: 'companyCode',
      fieldLabel: 'companyName',
      component: 'TreeSelect',
      componentProps: {
        api: companyTreeData,
        allowClear: true,
      },
      required: true,
    },
    {
      label: t('产品名称'),
      field: 'invName',
      component: 'Input',
      componentProps: {
        maxlength: 64,
      },
      required: true,
    },
    {
      label: t('邮发代号'),
      field: 'yfdh',
      component: 'Input',
      componentProps: {
        maxlength: 64,
      },
      required: true,
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, updateSchema, validate, getFieldsValue }] =
    useForm({
      labelWidth: 120,
      schemas: inputFormSchemas,
      baseColProps: { lg: 24, md: 24 },
    });
  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    console.log('data', data);
    await resetFields();
    setDrawerProps({ loading: true });
    // djDefaultValue.value = data.selIds
    setDrawerProps({ loading: false });
  });

  async function handleSubmit(event: any) {
    try {
      loadingRef.value = true;
      const data = await validate(); // 接受 BpmButton 传递过来的表单数据
      setDrawerProps({ confirmLoading: true });
      const res = await basInvUpdateYfdh(data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      loadingRef.value = false;
      setDrawerProps({ confirmLoading: false });
    }
  }

  async function handleSuccess() {
    emit('success');
  }
</script>
